﻿using Hardcodet.Wpf.TaskbarNotification;
using Microsoft.Extensions.DependencyInjection;
using QuickPasteToolWeb.Helper;
using QuickPasteToolWeb.Service;
using QuickPasteToolWeb.ViewModel;
using Serilog;
using Serilog.Core;
using System.Windows;

namespace QuickPasteToolWeb
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public IServiceProvider Services { get; }
        public new static App Current => (App)Application.Current;
        public static TaskbarIcon mTaskbarIcon;
        private static System.Threading.Mutex mutex;
        public App()
        {
            Services = ConfigServices();
        }
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            mTaskbarIcon = (TaskbarIcon)FindResource("Taskbar");
            var datasource = Services.GetService<SettingViewModel>();
            SettingHelper.InitSetting();
            var config = SettingHelper.GetSetting();
            datasource.Folder = config.Folder;
            datasource.ShowType = config.ShowType;
            datasource.IsOpenWebSearch = config.IsOpenWebSearch;
            mTaskbarIcon.DataContext = new TaskbarIconViewModel(new SettingWindow() { DataContext = datasource });

        }

        public static IServiceProvider ConfigServices()
        {
            var services = new ServiceCollection();
            services.AddSingleton<ILogger, Logger>();
            services.AddSingleton<IFileOperationService, FileOperationService>();
            services.AddSingleton<IWebClient, WebClient>();
            services.AddSingleton<SettingViewModel>();
            services.AddSingleton<MainWindowViewModel>();
            services.AddSingleton<MainWindow>(sp => new QuickPasteToolWeb.MainWindow { DataContext = sp.GetService<MainWindowViewModel>() });
            services.AddSingleton<SettingWindow>(sp => new QuickPasteToolWeb.SettingWindow { DataContext = sp.GetService<SettingViewModel>() });
            return services.BuildServiceProvider();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            var mainWindow = Services.GetService<MainWindow>();
            mainWindow.WindowStartupLocation = WindowStartupLocation.Manual;
            mainWindow.Top = 220;
            mainWindow.Left = 420;
            mainWindow.Show();
        }
    }

}
